using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CAccLevel : Class
    {
        #region Declares
        string _email = "";
        CGameLevels _gameLevel = null;
        int _score = -1;
        int _retest = -1;
        DateTime _timeBegin = DateTime.Now;
        DateTime _timeEnd = DateTime.Now;
        #endregion

        #region Constructors
        public CAccLevel()
        {
        }
        public CAccLevel(CLogin Login)
        {
            base.Login = Login;
        }
        public CAccLevel(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBAccLevel db = new AccDBAccLevel())
            {
                this.Populate((CAccLevel)db.GetClass(Login, ObjId));
            }
        }
        public CAccLevel(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CAccLevel(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBAccLevel db = new AccDBAccLevel())
            {
                this.Populate((CAccLevel)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("email") >= 0)
                    {
                        this._email = (dr["email"] != DBNull.Value ? Convert.ToString(dr["email"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD email [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("gameLevelId") >= 0)
                    {
                        this._gameLevel = new CGameLevels(Login, p, (dr["gameLevelId"] != DBNull.Value ? Convert.ToInt32(dr["gameLevelId"]) : -1));
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD gameLevelId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("score") >= 0)
                    {
                        this._score = (dr["score"] != DBNull.Value ? Convert.ToInt32(dr["score"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD score [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("retest") >= 0)
                    {
                        this._retest = (dr["retest"] != DBNull.Value ? Convert.ToInt32(dr["retest"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD retest [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("timeBegin") >= 0)
                    {
                        this._timeBegin = (dr["timeBegin"] != DBNull.Value ? Convert.ToDateTime(dr["timeBegin"]) : DateTime.Now);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD timeBegin [datetime] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("timeEnd") >= 0)
                    {
                        this._timeEnd = (dr["timeEnd"] != DBNull.Value ? Convert.ToDateTime(dr["timeEnd"]) : DateTime.Now);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblAccLevel ADD timeEnd [datetime] NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CAccLevel Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._email = Obj.email;
                    this._gameLevel = Obj.gameLevel;
                    this._score = Obj.score;
                    this._retest = Obj.retest;
                    this._timeBegin = Obj.timeBegin;
                    this._timeEnd = Obj.timeEnd;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBAccLevel db = new AccDBAccLevel())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBAccLevel db = new AccDBAccLevel())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBAccLevel db = new AccDBAccLevel())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBAccLevel db = new AccDBAccLevel())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CAccLevel obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CAccLevel AccLevel);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CAccLevel AccLevel);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CAccLevel AccLevel);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CAccLevel AccLevel);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CAccLevel obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public string email
        {
            get { return this._email; }
            set
            {
                if (this._email != value)
                {
                    this._email = value;
                }
            }
        }
        public CGameLevels gameLevel
        {
            get { return this._gameLevel; }
            set
            {
                if (this._gameLevel != value)
                {
                    this._gameLevel = value;
                }
            }
        }
        public int score
        {
            get { return this._score; }
            set
            {
                if (this._score != value)
                {
                    this._score = value;
                }
            }
        }
        public int retest
        {
            get { return this._retest; }
            set
            {
                if (this._retest != value)
                {
                    this._retest = value;
                }
            }
        }
        public DateTime timeBegin
        {
            get { return this._timeBegin; }
            set
            {
                if (this._timeBegin != value)
                {
                    this._timeBegin = value;
                }
            }
        }
        public DateTime timeEnd
        {
            get { return this._timeEnd; }
            set
            {
                if (this._timeEnd != value)
                {
                    this._timeEnd = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBAccLevel : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBAccLevel()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT AccLevelId, AccLevelName";
                sql += " FROM  tblAccLevel";
                sql += " WHERE (AccLevelId >= 0)";
                sql += " ORDER BY AccLevelName";
                DataTable table = p.OpenTable(sql, "tblAccLevel");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["AccLevelName"] = extName;
                        row["AccLevelId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "AccLevelName";
                    cmb.ValueMember = "AccLevelId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblAccLevel WHERE (AccLevelId = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblAccLevel");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CAccLevel(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CAccLevel AccLevel = (CAccLevel)obj;
            string sql = "SELECT * FROM tblAccLevel";
            sql += " WHERE email='" + AccLevel.email + "'";
            sql +=" AND gameLevelId =" + AccLevel.gameLevel.gameLevelId + "";
            DataTable dt = p.OpenTable(sql, "tblAccLevel");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["email"] = AccLevel.email;
                row["gameLevelId"] = AccLevel.gameLevel.gameLevelId;
                row["score"] = AccLevel.score;
                row["retest"] = AccLevel.retest;
                row["timeBegin"] = AccLevel.timeBegin;
                row["timeEnd"] = AccLevel.timeEnd;
                if (!p.UpdateTable("tblAccLevel", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CAccLevel AccLevel = (CAccLevel)obj;
            string sql = "DELETE FROM tblAccLevel";
            sql += " WHERE email='" + AccLevel.email + "'";
            sql += " AND gameLevelId =" + AccLevel.gameLevel.gameLevelId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblAccLevel";
            DataTable table = p.OpenTable(sql, "tblAccLevel");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CAccLevel o = new CAccLevel(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
            if (obj is CAccount)
            {
                string sql = "SELECT * FROM tblAccLevel";
                sql += " WHERE email='" + ((CAccount)obj).email + "' ORDER BY gameLevelId";
                DataTable table = p.OpenTable(sql, "tblAccLevel");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CAccLevel o = new CAccLevel(obj.Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="login"></param>
        /// <param name="Collection"></param>
        /// <returns></returns>
        public static bool ResetAll(CLogin Login, string email)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                p.OpenTransaction();
                string sql = "DELETE FROM tblAccLevel";
                sql += " WHERE email='" + email + "'";
                if (!p.ExecuteNonQuery(sql))
                {
                    p.Rollback();
                    p.CloseTransaction();
                    p.Close();
                    return false;
                }
                else
                {
                    p.CloseTransaction();
                    p.Close();
                }
                return true;
            }
            return false;
        }
    }
    #endregion
}
